<?php
namespace app\api\controller;

use app\common\controller\Api;

/**
 * 收藏接口
 */
class Favorites extends Api
{

    protected $noNeedRight = ['*'];
    // 测试不需要强制会员登陆
    protected $noNeedLogin = ['list'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 列表
     */
    public function list()
    {
        $page    = input('page', 1);
        $limit   = 10;
        $lx      = input('lx', '');
        // $where['user_id'] = $this->auth->id;

        // 测试数据
        $where['user_id'] = 83604;

        if (is_numeric($lx)) {
            $where['lx'] = $lx;
        }
        $favorites = model('favorites');

        $total = $favorites
            ->where($where)
            ->count();

        $list = $favorites->where($where)->order('id desc')->paginate($limit, $total, ['page' => $page]);
        if ($list) {
            foreach ($list as $k => $v) {
                $res['createtime'] = date('Y-m-d H:i', $v['createtime']);

                $newhouseLabelModel = model('NewhouseLabel');
                switch ( $v['lx'] ) {
                    case '0':
                        $news = model('News')->field('title,image')->where('id', $v['source_id'])->find();
                        $list[$k]['newsTitle'] = $news->title;
                        $newsImage = explode(',', $news->image);
                        $list[$k]['image'] = $newsImage[0];
                        break;
                    case '1':
                        $newhouse = model('Newhouse')->field('title,partnerlabel_id,covertype,houselabel_ids,house_image,businesslabel_ids,business_image')->where('id', $v['source_id'])->find();
                        $newhouseLabel = $newhouseLabelModel->where('id', $newhouse->partnerlabel_id)->field('name,class')->find();
                        $list[$k]['newhouseLabel'] = $newhouseLabel->name;
                        $list[$k]['newhouseLabelClass'] = $newhouseLabel->class;
                        // 0=住宅,1=商业
                        if ( $newhouse->covertype ) {
                            // 商业
                            $list[$k]['newhouseBusinessLabel'] = $newhouseLabelModel->where('id', 'in', $newhouse->businesslabel_ids)->field('name,class')->select();
                            $list[$k]['image'] = $newhouse->business_image;
                        } else {
                            // 住宅
                            $list[$k]['newhouseHouseLabel'] = $newhouseLabelModel->where('id', 'in', $newhouse->houselabel_ids)->field('name,class')->select();
                            $list[$k]['image'] = $newhouse->house_image;
                        }
                    break;
                    case '2':
                        $newhouseLayout = model('NewhouseLayout')->field('newhouse_id,title,attribute,label_ids,url')->where('id', $v['source_id'])->find();
                        $newhouseName = model('Newhouse')->field('title')->where('id', $newhouseLayout->newhouse_id)->find();
                        $list[$k]['newhouseName'] = $newhouseName->title;
                        $list[$k]['newhouseLayoutTitle'] = $newhouseLayout->title;
                        $list[$k]['attribute'] = $newhouseLayout->attribute;
                        $list[$k]['attribute_text'] = $newhouseLayout->attribute_text;
                        $list[$k]['newhouseLayoutLabel'] = $newhouseLabelModel->where('id', 'in', $newhouseLayout->label_ids)->field('name,class')->select();
                        $newhouseLayoutImages = explode(',', $newhouseLayout->url);
                        $list[$k]['image'] = $newhouseLayoutImages[0];
                    break;
                    case '3':
                        // 'Zx 毛坯'  =>  '尚未装修',
                        // 'Zx 简易'  =>  '简易装修',
                        // 'Zx 中档'  =>  '中档装修',
                        // 'Zx 高档'  =>  '高档装修',
                        $esfHouse = model('EsfHouse')->field('title,images,fanjia,jiawei,htype,allarea,zx')->where('id', $v['source_id'])->find();
                        $list[$k]['esfHouseTitle'] = $esfHouse->title;
                        $esfHouseImages = explode(',', $esfHouse->images);
                        $list[$k]['image'] = $esfHouseImages[0];
                        $jiawei = $esfHouse->jiawei ? '万元' : '元';
                        $list[$k]['esfHouseFanjia'] = $esfHouse->fanjia . $jiawei;
                        $list[$k]['esfHouseHtype'] = $esfHouse->htype;
                        $list[$k]['esfHouseAllarea'] = $esfHouse->allarea . '';
                        switch ( $esfHouse->zx ) {
                            case '毛坯':
                                $esfHouseZx = '尚未装修';
                                break;
                            case '简易':
                                $esfHouseZx = '简易装修';
                                break;
                            case '中档':
                                $esfHouseZx = '中档装修';
                                break;
                            case '高档':
                                $esfHouseZx = '高档装修';
                                break;
                        }
                        $list[$k]['esfHouseZx'] = $esfHouseZx;
                        break;
                    case '4':
                        $esfHouse = model('EsfHouse')->field('title,images,fanjia,jiawei,htype,allarea,zx')->where('id', $v['source_id'])->find();
                        $list[$k]['esfHouseTitle'] = $esfHouse->title;
                        $esfHouseImages = explode(',', $esfHouse->images);
                        $list[$k]['image'] = $esfHouseImages[0];
                        $jiawei = $esfHouse->jiawei ? '万元' : '元';
                        $list[$k]['esfHouseFanjia'] = $esfHouse->fanjia . $jiawei;
                        $list[$k]['esfHouseHtype'] = $esfHouse->htype;
                        $list[$k]['esfHouseAllarea'] = $esfHouse->allarea;
                        switch ( $esfHouse->zx ) {
                            case '毛坯':
                                $esfHouseZx = '尚未装修';
                                break;
                            case '简易':
                                $esfHouseZx = '简易装修';
                                break;
                            case '中档':
                                $esfHouseZx = '中档装修';
                                break;
                            case '高档':
                                $esfHouseZx = '高档装修';
                                break;
                        }
                        $list[$k]['esfHouseZx'] = $esfHouseZx;
                        break;
                }
            }

            $this->success('操作成功', $list);
        } else {
            $this->error('请求错误');
        }
    }

    /**
     * 我的收藏记录总数
     */
    public function mycount()
    {
        $favorites = model('favorites');
        $where['user_id'] = $this->auth->id;
        $total = $favorites
            ->where($where)
            ->count();

        $this->success('操作成功', $total);
    }

    /**
     * 检查收藏
     */
    public function check()
    {
        $pid = input('pid');
        $lx = input('lx');
        $where['source_id'] = $pid;
        $where['user_id'] = $this->auth->id;
        $where['lx'] = $lx;

        $favorites = model('favorites');
        $res = $favorites->where($where)->find();

        if ($res) {
            $this->success('操作成功', $res);
        } else {
            $this->error('请求错误');
        }
    }

    /**
     * 关注
     */
    public function follow()
    {
        $pid    = input('pid');
        $title  = input('title');
        $lx     = input('lx');
        $data['source_id'] = $pid;
        $data['user_id'] = $this->auth->id;
        $data['title'] = $title;
        $data['lx'] = $lx;
        $favorites = model('favorites');

        $res = $favorites->allowField(true)->save($data);
        if ($res !== false) {
            $this->success('操作成功');
        } else {
            $this->error('请求错误');
        }
    }

    /**
     * 取消关注
     */
    public function unfollow()
    {
        $pid    = input('pid');
        $lx     = input('lx');
        $where['source_id'] = $pid;
        $where['user_id'] = $this->auth->id;
        $where['lx'] = $lx;
        $favorites = model('favorites');

        $res = $favorites->where($where)->delete();
        if ($res !== false) {
            $this->success('操作成功');
        } else {
            $this->error('请求错误');
        }
    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = input('id');
        $where['id'] = $id;
        $where['user_id'] = $this->auth->id;
        $favorites = model('favorites');

        $res = $favorites->where($where)->delete();
        if ($res !== false) {
            $this->success('操作成功');
        } else {
            $this->error('请求错误');
        }
    }

}
